package com.oreilly.ez.web;


import cn.hutool.core.lang.Assert;
import com.oreilly.ez.base.R;
import com.oreilly.ez.entity.CourseTerm;
import com.oreilly.ez.entity.Study;
import com.oreilly.ez.service.StudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

@RestController
@RequestMapping("/studies")
public class StudyCtrl  extends BaseCtrl<Study> {

    @Autowired
    private StudyService studyService;


    /**
     * 新增, 修改
     * @param item
     * @return
     */
    public Object save(@RequestBody Study item){

        Assert.notNull(item, "保存对象不能为空");

        CourseTerm courseTerm = item.getCourseTerm();
        Assert.notNull(courseTerm, "开班信息不能为空");
        BigDecimal fee = courseTerm.getFee();
        item.setFee(fee);
        item.setPayFee(fee);

        return studyService.save(item);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Object delete(@PathVariable Integer id) {

        studyService.remove(id);
        return R.ok("delete success");
    }


    /**
     * 查询
     * @param item
     * @param pageable
     * @return
     */
    @Override
    public Object find(Study item, @PageableDefault Pageable pageable){

        return studyService.find(item,  pageable);
    }


    /**
     *
     * 查看某个 开班的 报名学生
     *
     * @param termId
     * @return
     */

    @GetMapping("/find-by-term-id/{id}")
    public Object findByTermId(@PathVariable("id") Integer termId){

        return studyService.findByTermId(termId);
    }


    /**
     *
     * 查看某个 学生的 所有报名班级
     *
     * @param stuId
     * @return
     */

    @GetMapping("/find-by-stu-id/{id}")
    public Object findByStuId(@PathVariable("id") Integer stuId){

        return studyService.findByStuId(stuId);
    }


    /**
     * 查询 by id
     * @param id
     * @return
     */
    @Override
    public Object findOne(@PathVariable Integer id){

        return studyService.findOne(id);
    }


}
